<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  import type { BlogPost } from '@/types/blog'

  const route = useRoute()
  const router = useRouter()

  const loading = ref(false)
  const searchQuery = ref('')
  const searchResults = ref<BlogPost[]>([])
  const searchTime = ref(0)

  // 模拟搜索结果
  const mockResults: BlogPost[] = [
    {
      id: '1',
      title: 'Vue 3 Composition API 深入理解',
      summary: '深入探讨 Vue 3 Composition API 的核心概念和最佳实践。',
      publishDate: '2024-01-15',
      category: 'Vue.js',
      tags: ['Vue3', 'JavaScript'],
      readTime: '10分钟',
      views: 1234,
      likes: 89,
    },
  ]

  const performSearch = async (query: string) => {
    if (!query.trim()) return

    try {
      loading.value = true
      const startTime = Date.now()

      // 模拟API调用
      await new Promise(resolve => setTimeout(resolve, 500))

      searchResults.value = mockResults.filter(
        post =>
          post.title.toLowerCase().includes(query.toLowerCase()) ||
          post.summary.toLowerCase().includes(query.toLowerCase())
      )

      searchTime.value = Date.now() - startTime
    } catch (error) {
      console.error('搜索失败:', error)
    } finally {
      loading.value = false
    }
  }

  const handleSearch = () => {
    if (searchQuery.value.trim()) {
      performSearch(searchQuery.value)
      router.push({ query: { q: searchQuery.value } })
    }
  }

  const handlePostClick = (postId: string) => {
    router.push(`/articles/${postId}`)
  }

  onMounted(() => {
    const query = route.query.q as string
    if (query) {
      searchQuery.value = query
      performSearch(query)
    }
  })
</script>

<template>
  <div class="search-page">
    <div class="container">
      <!-- 搜索框 -->
      <div class="search-section">
        <h1 class="page-title">🔍 搜索</h1>
        <div class="search-box">
          <input
            v-model="searchQuery"
            type="text"
            placeholder="搜索文章标题或内容..."
            class="search-input"
            @keyup.enter="handleSearch"
          />
          <button class="search-btn" :disabled="loading" @click="handleSearch">
            {{ loading ? '搜索中...' : '搜索' }}
          </button>
        </div>
      </div>

      <!-- 搜索结果 -->
      <div v-if="searchQuery && !loading" class="results-section">
        <div class="results-info">
          <span>找到 {{ searchResults.length }} 个结果</span>
          <span class="search-time">(用时 {{ searchTime }}ms)</span>
        </div>

        <div v-if="searchResults.length === 0" class="no-results">
          <div class="no-results-icon">🔍</div>
          <h3>没有找到相关结果</h3>
          <p>尝试使用不同的关键词进行搜索</p>
        </div>

        <div v-else class="results-list">
          <article
            v-for="post in searchResults"
            :key="post.id"
            class="result-item"
            @click="handlePostClick(post.id)"
          >
            <h3 class="result-title">{{ post.title }}</h3>
            <p class="result-summary">{{ post.summary }}</p>
            <div class="result-meta">
              <span class="category">{{ post.category }}</span>
              <span class="publish-date">
                {{ new Date(post.publishDate).toLocaleDateString('zh-CN') }}
              </span>
              <span class="read-time">{{ post.readTime }}</span>
            </div>
          </article>
        </div>
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading">
        <div class="loading-spinner"></div>
        <p>正在搜索...</p>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
  .search-page {
    min-height: 100vh;
    padding: 2rem 0;
  }

  .search-section {
    text-align: center;
    margin-bottom: 3rem;
  }

  .page-title {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0 0 2rem 0;
  }

  .search-box {
    max-width: 600px;
    margin: 0 auto;
    display: flex;
    border: 2px solid var(--border-color);
    border-radius: var(--radius-xl);
    overflow: hidden;
    box-shadow: var(--shadow-md);

    &:focus-within {
      border-color: var(--primary-color);
    }
  }

  .search-input {
    flex: 1;
    padding: 1rem 1.5rem;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 1rem;

    &::placeholder {
      color: var(--text-tertiary);
    }

    &:focus {
      outline: none;
    }
  }

  .search-btn {
    padding: 1rem 2rem;
    border: none;
    background: var(--primary-color);
    color: white;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s ease;

    &:hover:not(:disabled) {
      background: var(--primary-hover);
    }

    &:disabled {
      opacity: 0.6;
      cursor: not-allowed;
    }
  }

  .results-section {
    max-width: 800px;
    margin: 0 auto;
  }

  .results-info {
    margin-bottom: 1.5rem;
    color: var(--text-secondary);
    font-size: 0.9rem;
  }

  .search-time {
    color: var(--text-tertiary);
  }

  .no-results {
    text-align: center;
    padding: 3rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-xl);

    .no-results-icon {
      font-size: 4rem;
      margin-bottom: 1rem;
    }

    h3 {
      color: var(--text-primary);
      margin-bottom: 0.5rem;
    }

    p {
      color: var(--text-secondary);
      margin: 0;
    }
  }

  .results-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .result-item {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
      border-color: var(--primary-color);
      box-shadow: var(--shadow-md);
      transform: translateY(-2px);
    }
  }

  .result-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 0.75rem 0;
    line-height: 1.4;

    &:hover {
      color: var(--primary-color);
    }
  }

  .result-summary {
    color: var(--text-secondary);
    line-height: 1.5;
    margin: 0 0 1rem 0;
  }

  .result-meta {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.85rem;
    color: var(--text-tertiary);
    flex-wrap: wrap;
  }

  .category {
    background: var(--primary-color);
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
  }

  .loading {
    text-align: center;
    padding: 3rem;
    color: var(--text-secondary);
  }

  .loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top: 3px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
  }

  @keyframes spin {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .page-title {
      font-size: 2rem;
    }

    .search-box {
      flex-direction: column;
    }

    .search-btn {
      padding: 0.75rem;
    }

    .result-meta {
      flex-direction: column;
      align-items: flex-start;
      gap: 0.5rem;
    }
  }

  @media (max-width: 480px) {
    .search-page {
      padding: 1rem 0;
    }

    .page-title {
      font-size: 1.75rem;
    }

    .result-item {
      padding: 1.25rem;
    }
  }
</style>
